<?php
include('include/config.inc.php');
$companyId   = "";
$companyName = "";
$fieldName   = "";
  ///// Select user : Start
if(isset($_REQUEST['Challan']) && ($_REQUEST['Challan'] == 'Goods Inward Note' || $_REQUEST['Challan'] == 'Goods Rejection'))
{
	$selectParty = "SELECT DISTINCT deliverychallanmaster.supplierId,supplier.supplierId,supplier.companyName
	                  FROM deliverychallanmaster
	                  JOIN supplier ON supplier.supplierId = deliverychallanmaster.supplierId
	                 ORDER BY companyName";
	$selectPartyResult = mysql_query($selectParty);
  $a = 0;
  while($PartyRow = mysql_fetch_array($selectPartyResult))
  { 
    $companyId[$a]   = $PartyRow['supplierId'];
    $companyName[$a] = $PartyRow['companyName'];
    $a++;
  }
}
elseif(isset($_REQUEST['Challan']) && $_REQUEST['Challan'] == 'Goods Return Note')
{
 $selectParty = "SELECT DISTINCT deliverychallanmaster.supplierId AS sid,0 AS pid,supplier.supplierId,supplier.companyName
		                 FROM deliverychallanmaster
		                 JOIN deliverychallandetail ON deliverychallandetail.deliveryChallanMasterId = deliverychallanmaster.deliveryChallanMasterId
                     JOIN goodsreturn ON goodsreturn.deliveryChallanDetailId = deliverychallandetail.deliveryChallanDetailId
		                 JOIN supplier ON supplier.supplierId = deliverychallanmaster.supplierId
		                UNION 
		               SELECT DISTINCT 0 AS sid,deliverychallanmaster.partyId AS pid,party.partyId,party.companyName
                     FROM deliverychallanmaster
                     JOIN party ON party.partyId = deliverychallanmaster.partyId
                     JOIN deliverychallandetail ON deliverychallandetail.deliveryChallanMasterId = deliverychallanmaster.deliveryChallanMasterId
                     JOIN goodsreturn ON goodsreturn.deliveryChallanDetailId = deliverychallandetail.deliveryChallanDetailId";
	$selectPartyResult = mysql_query($selectParty);
  $a = 0;
  while($partyRow = mysql_fetch_array($selectPartyResult))
  { 
    if($partyRow['sid'] != 0)
    {
      $companyId[$a]     = $partyRow['sid']."S";
    }
    else
    {
    	$companyId[$a]     = $partyRow['pid']."P";
    }
    $companyName[$a] = $partyRow['companyName'];
    $a++;
  }
}
else
{
  $selectParty .= "SELECT DISTINCT deliverychallanmaster.partyId,supplier.supplierId AS partyId,supplier.companyName
				  						         FROM deliverychallanmaster
					 						         JOIN user ON user.userId = deliverychallanmaster.userId
						 					         JOIN supplier ON supplier.supplierId = deliverychallanmaster.supplierId
                                AND deliverychallanmaster.status = 'A' 
                                AND deliverychallanmaster.challanType = '".$_REQUEST['Challan']."' ";
  $selectParty .= " UNION ";
  $selectParty .= " SELECT DISTINCT deliverychallanmaster.partyId,party.partyId,party.companyName
                    FROM deliverychallanmaster
                    JOIN party ON party.partyId = deliverychallanmaster.partyId
                   WHERE deliverychallanmaster.challanType = '".$_REQUEST['Challan']."'
                   ORDER BY companyName";
 $selectPartyResult = mysql_query($selectParty);
 $a = 0;
 while($PartyRow = mysql_fetch_array($selectPartyResult))
 { 
   $companyId[$a]   = $PartyRow['partyId'];
   $companyName[$a] = $PartyRow['companyName'];
   $a++;
 }
}

include("./bottom.php");
$smarty->assign("companyId",$companyId);
$smarty->assign("companyName",$companyName);
$smarty->display("partyCustAj.tpl");
?>